package com.wu.family.publish.photo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.co.cyberagent.android.gpuimage.GPUImageFilter;

import com.wu.family.R;
import com.wu.family.message.MsgDialogueAdapter.ViewHolder;
import com.wu.family.publish.photo.utils.GPUImageFilterTools;
import com.wu.family.utils.DisplayUtil;
import com.wu.family.utils.FilesTool;
import com.wu.family.utils.ToastUtil;
import com.wu.family.utils.ze.LoadSDcardImageMgr;
import com.wu.family.views.MySurfaceView.OnPictureTokenInterface;



import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class PhotoFilterAdapter extends BaseAdapter {
	private Context mContext; 
	private List<String> filterTypes = null;
	private List<Integer> drawableIds = null;
	
	private int selectPos = 0;
	public PhotoFilterAdapter(Context context) {
		mContext = context;
		this.filterTypes = GPUImageFilterTools.getFilterList().names;
		
		drawableIds = new ArrayList<Integer>();
		drawableIds.add(R.drawable.filter00);
		drawableIds.add(R.drawable.filter01);
		drawableIds.add(R.drawable.filter02);
		drawableIds.add(R.drawable.filter03);
		drawableIds.add(R.drawable.filter04);
		drawableIds.add(R.drawable.filter05);
		drawableIds.add(R.drawable.filter06);
		drawableIds.add(R.drawable.filter07);
		drawableIds.add(R.drawable.filter08);
		drawableIds.add(R.drawable.filter09);
		drawableIds.add(R.drawable.filter10);
		drawableIds.add(R.drawable.filter11);
		drawableIds.add(R.drawable.filter12);
		drawableIds.add(R.drawable.filter13);
		drawableIds.add(R.drawable.filter14);
		drawableIds.add(R.drawable.filter15);
		drawableIds.add(R.drawable.filter16);
		drawableIds.add(R.drawable.filter17);
		drawableIds.add(R.drawable.filter18);
		drawableIds.add(R.drawable.filter19);
		drawableIds.add(R.drawable.filter20);
		drawableIds.add(R.drawable.filter21);
		drawableIds.add(R.drawable.filter22);
		drawableIds.add(R.drawable.filter23);
				
	}
	
	@Override
	public int getCount() {
		return filterTypes != null?(filterTypes.size()):0;
	}

	@Override
	public Object getItem(int arg0) {
		return filterTypes != null?filterTypes.get(arg0):null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View convertView, ViewGroup arg2) {
		final ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.editor_filter_item, null);
			holder.llLlPic = (LinearLayout) convertView.findViewById( R.id.llPic );
			holder.ivIvPic = (ImageView) convertView.findViewById( R.id.ivPic );
			holder.tvTvFilterType = (TextView) convertView.findViewById( R.id.tvFilterType );

			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		String type= filterTypes.get(arg0);
		holder.tvTvFilterType.setText(type);
		if (selectPos == arg0) {
			holder.llLlPic.setBackgroundColor(Color.parseColor("#9ED54A"));
		}else {
			holder.llLlPic.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
		}
		
		holder.ivIvPic.setImageResource(drawableIds.get(arg0));
		/*holder.llLlPic.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				selectPos = arg0;
				notifyDataSetChanged();
				if (onFilterChosenListener != null) {
					onFilterChosenListener.onFilterChosenListener(
							GPUImageFilterTools.createFilter(mContext, arg0));
				}
			}
		});*/
		
		return convertView;
	}
	
	
	public final class ViewHolder{
		private LinearLayout llLlPic;
		private ImageView ivIvPic;
		private TextView tvTvFilterType;
	}
	
	OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			selectPos = arg2;
			notifyDataSetChanged();
			if (onFilterChosenListener != null) {
				onFilterChosenListener.onFilterChosenListener(
						GPUImageFilterTools.createFilter(mContext, arg2));
			}
		}
	};
	
	public void setOnFilterChosenListener(
			OnFilterChosenListener onFilterChosenListener) {
		this.onFilterChosenListener = onFilterChosenListener;
	}
	OnFilterChosenListener onFilterChosenListener = null;
	public interface OnFilterChosenListener {
        void onFilterChosenListener(GPUImageFilter filter);
    }
}
